package com.tmall.tmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tmall.tmall.mapper.AdvertisingMapper;
import com.tmall.tmall.pojo.Advertising;
import com.tmall.tmall.pojo.PageRt;
import com.tmall.tmall.pojo.Slideshow;
import com.tmall.tmall.service.AdvertisingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdvertisingServiceImpl implements AdvertisingService {
    @Autowired
    private AdvertisingMapper advertisingMapper;

    @Override
    public void insert(Advertising advertising) {
        advertising.setCreateTime(LocalDateTime.now());
        advertisingMapper.insert(advertising);
    }

    @Override
    public Advertising getByPosition(Integer position) {
        return advertisingMapper.getByPosition(position);
    }

    @Override
    public PageRt page(Integer pageNum, Integer pageSize, Integer position) {
        PageHelper.startPage(pageNum, pageSize);
        List<Advertising> list = advertisingMapper.page(position);
        PageInfo<Advertising> pageInfo = new PageInfo<>(list);
        return new PageRt(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void update(Advertising advertising) {
        advertisingMapper.update(advertising);
    }

    @Override
    public void delete(List<Integer> ids) {
        for (Integer id : ids) {
            Advertising advertising = Advertising.builder().id(id).deleteAt(LocalDateTime.now()).build();
            advertisingMapper.update(advertising);
        }
    }

    @Override
    public List<Advertising> selectAll() {
        return advertisingMapper.selectAll();
    }
}
